<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="文件名称" prop="ossName">
        <el-input
          v-model="queryParams.ossName"
          placeholder="请输入文件名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['aliyunoss:oss:add']"
        >添加</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ossList">
      <el-table-column label="文件编号" prop="ossId"/>
      <el-table-column label="文件名称" prop="ossName"/>
      <el-table-column label="文件地址" prop="ossUrl"/>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['aliyunoss:oss:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['aliyunoss:oss:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文件信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item v-if="form.ossId !== undefined"  label="文件编号">
              <el-input v-model="form.ossId" type="input" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="文件名称">
              <el-input v-model="form.ossName" type="input" placeholder="请输入文件名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row label="上传模板">
          <el-upload
            multiple
            :action="uploadFileUrl"
            :limit="1"
            :on-error="handleUploadError"
            :on-success="handleUploadSuccess"
            :show-file-list="false"
            :headers="headers"
            class="upload-file-uploader"
            ref="upload"
          >
            <!-- 上传按钮 -->
            <el-button size="mini" type="primary" style="margin: 10px 80px 20px 80px;">选取文件</el-button>
          </el-upload>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOss, updateOss, addOss, deleteOss } from "@/api/aliyunoss/oss";
import { getToken } from "@/utils/auth";

export default {
  name: "Teacher",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ossName: undefined
      },
      ossList: [],
      title: "",
      // 表单参数
      form: {},
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/aliyunoss/oss/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询教师列表 */
    getList() {
      this.loading = true;
      listOss(this.queryParams).then(response => {
          this.ossList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置 */
    resetQuery() {
      this.queryParams.ossName = undefined;
      this.ossList = [];
      this.handleQuery();
    },
    /** 新增（导入） */
    handleAdd () {
      this.title = "添加课程信息";
      this.form = {};
      this.open = true;
    },
    handleUpdate(row) {
      this.title = "修改文件信息";
      this.form = row;
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ossIds = row.ossId || this.ids;
      this.$modal.confirm('是否确认删除文件编号为"' + ossIds + '"的数据项？').then(function() {
        return deleteOss(ossIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.form = {};
    },
    /** 提交按钮 */
    submitForm: function() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ossId != undefined) {
            updateOss(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }else {
            addOss(this.form).then(res => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            })
          }
        }
      });
    },
    // 上传失败
    handleUploadError(err) {
      this.$modal.msgError("上传图片失败，请重试");
    },
    // 上传成功回调
    handleUploadSuccess(res, file) {
      if (res.code === 200) {
        this.form.ossUrl = res.data
      } else {
        this.$modal.msgError(res.msg);
      }
    },
  }
};
</script>
